<?php
/*
 * ebiz front
 *
 * Customer Model
 *
 * @author li.jf
 *
 */

require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class OrderItemDao extends BaseDao {

    private $tableName = "order_items";

    public function __construct($dbUtil){
        parent::__construct($dbUtil);
    }

     //get productiteminfo not in any orderpackages
     public function getOrderItemsInfoNotInAnyPackages($orderid){
        $sql="select oi.id,oi.quantity,oi.sale_price,oi.deal_price,pri.id as pid,pri.attribute_1,pri.value_1,pri.attribute_2,pri.value_2,
                pri.attribute_3,pri.value_3,pri.attribute_4,pri.value_4,pri.attribute_5,pri.value_5,p.title,p.url_title
                from $this->tableName 		oi
                left outer join product_items           pri     on   oi.item_id		  = pri.id
                left outer join products                p       on    p.id 			  = pri.product_id
                where  oi.order_id=? and oi.is_delete=0 and oi.id not in(
                                         select  opi.order_item_id   from order_package_items opi
                                         where opi.is_delete = 0
                                )";
        $param = array($orderid);
        return $this->dbUtil->SelectData($sql, $param);
    }


	/**
	 * @author allen
	 */

    /**
     * save the order item info from checkout
     */
    public function saveOrderItemInfo($param){
        $sql = "insert into $this->tableName
        (order_id, item_id, status, quantity, sale_price, deal_price, discount, tax, cheque_rule, order_profit, ship_profit, cheque_status, modify_id, create_id, modified, created)
        values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,now(),now())";
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }

    /**
     * get the info by id
     */
    public function getOrderInfoByOrderId($orderId){
        $sql="select * from $this->tableName where order_id=? and is_delete = 0 ";
        $param = array($orderId);
        return $this->dbUtil->SelectData($sql, $param);
    }

    /**
     * get the item info to show in success order page
     */
    public function getItemInfoByOrderId($orderId){
        $param = array($orderId);
        $sql = "select pi.id as pItemId ,
                pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 ,
				oi.`discount`, oi.`ship_profit`, oi.`cheque_rule`, oi.`tax`, oi.cheque_status,
				oi.id as orderItemId , oi.quantity , oi.sale_price  ,  oi.deal_price , p.title , p.url_title
				from  product_items pi
				join products p on p.id = pi.product_id
				left join $this->tableName oi on oi.item_id = pi.id
				where oi.order_id = ?  and oi.is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
    }

    /**
     * get the item info to show in success order page
     */
    public function getItemInfoByOrderIdByWang($orderId){
        $param = array($orderId);
        $sql = "select pi.id as pItemId ,
                pi.attribute_1 , pi.value_1 , pi.attribute_2 , pi.value_2 ,  pi.attribute_3 , pi.value_3 ,  pi.attribute_4 , pi.value_4,  pi.attribute_5, pi.value_5 ,
				oi.`discount`, oi.`ship_profit`, oi.`cheque_rule`, oi.`tax`, oi.cheque_status,oi.`upc`, oi.`item_name`,
				oi.id as orderItemId , oi.quantity , oi.sale_price  ,  oi.deal_price , p.title , p.url_title
				from $this->tableName oi
				left join product_items pi on oi.item_id = pi.id
				left join products p on p.id = pi.product_id
				where oi.order_id = ?  and oi.is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
    }

    /*
     * get the success order item number by item id
     */
    public function getSuccessOrderItemNumByItemId($itemId){
    	$param = array($itemId);
    	$sql = "select count(*)  from $this->tableName oi
				join  orders o  on o.id = oi.order_id
				where   o.status in ( 5,15,25 ) and oi.item_id = 1";
		return $this->dbUtil->SelectData($sql, $param);
    }
    
    /**
     * 显示所有评论页面使用，根据itemids找到最新的一个orderid
     */
    public function getLatestOrderInfoByItemIds($itemIds){
    	$param = array();
        $sql = "select *  from $this->tableName 
                where item_id in ( $itemIds ) and is_delete = 0 order by created desc limit 0,1";
        return $this->dbUtil->SelectData($sql, $param);
    }

}